package org.shop.controller.bbsManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Merchant;
import org.shop.entity.PostInfo;
import org.shop.entity.SystemMessage;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.MerchantService;
import org.shop.service.PostInfoService;
import org.shop.service.SystemMessageService;
import org.shop.service.UserService;
import org.shop.util.MD5Util;
import org.shop.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class bbsManagerPostController {

	private static Logger logger = LogManager.getLogger(bbsManagerPostController.class);
	
	@Autowired
    private PostInfoService postInfoService;
	
	
	/**
	 * 获取论坛信息列表
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/getPostInfoList")
	@ResponseBody
	public Map<String,Object> getPostInfoList(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String content = req.getParameter("content");
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("content", content);
            map = postInfoService.getPostInfoList(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 获取帖子的照片
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/getPostInfoImg")
	@ResponseBody
	public Map<String,Object> getPostInfoDetails(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
		try {
			PostInfo postInfo = postInfoService.selectByPrimaryKey(commentsId);
			String imgPost = postInfo.getContentImg();
			String[] imgList = imgPost.split(",");
			String url = "http://" + req.getServerName() + ":"+ req.getServerPort()+ req.getContextPath()+"/";
			for(int i = 0;i<imgList.length;i++) {
				Map<String,Object> map2 = new HashMap<String,Object>();
				map2.put("alt", "图片"+(i+1));
				map2.put("pid", i);
				map2.put("src", url+"common/getGoodsPhoto?photoUrl="+imgList[i]);
				map2.put("thumb", url+"common/getGoodsPhoto?photoUrl="+imgList[i]);
				data.add(map2);
			}
			map.put("title", "帖子相册");
			map.put("id", commentsId);
			map.put("start", 0);
			map.put("data", data);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 关闭帖子
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/closePostInfo")
	@ResponseBody
	public int closePostInfo(HttpServletRequest req) {
		int num = 0;
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		try {
			PostInfo postInfo = new PostInfo();
			postInfo.setCommentsId(commentsId);
			postInfo.setState((byte) 3);
			postInfo.setOpTime(new Date());
			num = postInfoService.updateByPrimaryKeySelective(postInfo );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	/**
	 * 打开帖子
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/openPostInfo")
	@ResponseBody
	public int openPostInfo(HttpServletRequest req) {
		int num = 0;
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		try {
			PostInfo postInfo = new PostInfo();
			postInfo.setCommentsId(commentsId);
			postInfo.setState((byte) 1);
			postInfo.setOpTime(new Date());
			num = postInfoService.updateByPrimaryKeySelective(postInfo );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	/**
	 * 置顶
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/openIsTop")
	@ResponseBody
	public int openIsTop(HttpServletRequest req) {
		int num = 0;
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		try {
			PostInfo postInfo = new PostInfo();
			postInfo.setCommentsId(commentsId);
			postInfo.setIsTop((byte) 1);
			postInfo.setToTopTime(new Date());
			num = postInfoService.updateByPrimaryKeySelective(postInfo );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	
	/**
	 * 帖子取消置顶
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/cancelIsTop")
	@ResponseBody
	public int cancelIsTop(HttpServletRequest req) {
		int num = 0;
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		try {
			PostInfo postInfo = new PostInfo();
			postInfo.setCommentsId(commentsId);
			postInfo.setIsTop((byte) 0);
			num = postInfoService.updateByPrimaryKeySelective(postInfo );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	
	/**
	 * 帖子加精
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/openIsGood")
	@ResponseBody
	public int openIsGood(HttpServletRequest req) {
		int num = 0;
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		try {
			PostInfo postInfo = new PostInfo();
			postInfo.setCommentsId(commentsId);
			postInfo.setIsGood((byte) 1);
			postInfo.setIsGoodsTime(new Date());
			num = postInfoService.updateByPrimaryKeySelective(postInfo );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	/**
	 * 帖子取消加精
	 * @param req
	 * @return
	 */
	@RequestMapping("bbsManager/cancelIsGood")
	@ResponseBody
	public int cancelIsGood(HttpServletRequest req) {
		int num = 0;
		Long commentsId = Long.parseLong(req.getParameter("commentsId"));
		try {
			PostInfo postInfo = new PostInfo();
			postInfo.setCommentsId(commentsId);
			postInfo.setIsGood((byte) 0);
			num = postInfoService.updateByPrimaryKeySelective(postInfo );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	
	
}
